//
//  ST_GiveGiftCrowdView.swift
//  ST_GiveGiftCrowdView
//
//  Created by Jason on 2021/8/26.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift

class ST_GiveGiftCrowdView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.equalTo(44)
        }
        
        self.addSubview(select_option_btn)
        select_option_btn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview()
            make.width.equalTo(70)
            make.height.equalTo(34)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - Properties
    fileprivate lazy var collectionView:UICollectionView = {
        
        let control = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: self.layout)
        control.delegate = self
        control.dataSource = self
        control.register(cellWithClass: ST_GiveGiftCrowdCell.self)
        
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.backgroundColor = UIColor.clear
        control.alwaysBounceHorizontal = true
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        return control
    }()
    
    fileprivate lazy var layout:UICollectionViewFlowLayout = {
        let controlLayout = UICollectionViewFlowLayout()
        controlLayout.scrollDirection = .horizontal
        controlLayout.minimumLineSpacing = 15
//        controlLayout.minimumInteritemSpacing = 9
        
        controlLayout.sectionInset = UIEdgeInsets.init(top: 0, left: 15, bottom: 0, right: 100)
        
        controlLayout.itemSize = CGSize.init(width: 42, height: 44)
        
        return controlLayout
    }()
    
    fileprivate lazy var select_option_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitle("全选", for: .normal)
        control.setTitle("取消", for: .selected)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.backgroundColor = Color.init(hexString: "#FEC207")
        control.setTitleColorForAllStates(UIColor.init(hex: 0x202223)!)
        control.cornerRadius = 17
        control.addTarget(self, action: #selector(select_option_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    var data_array = [ST_GiveGiftCrowdCellModel](){
        didSet{
            collectionView.reloadData()
        }
    }
    
    @objc fileprivate func select_option_handle(){
        
        select_option_btn.isSelected = !select_option_btn.isSelected
        
        data_array.forEach({ $0.isSelected = select_option_btn.isSelected })
        
        collectionView.reloadData()
        
    }
    
}


extension ST_GiveGiftCrowdView:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withClass: ST_GiveGiftCrowdCell.self, for: indexPath)
        
        cell.model = data_array[safe:indexPath.item]
        
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return data_array.count
    }
    
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        
        guard let last = data_array[safe: indexPath.item]?.isSelected else{
            return
        }
            
        
        data_array[safe: indexPath.item]?.isSelected = !last
        
        collectionView.performBatchUpdates {
            
            collectionView.reloadItems(at: [indexPath])
            
        } completion: { _ in
            
        }
        
    }
    
}


class ST_GiveGiftCrowdCell: UICollectionViewCell {
    
    override init(frame: CGRect){
        super.init(frame: frame)
        
        contentView.addSubview(select_sign_view)
        select_sign_view.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(40)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    fileprivate lazy var avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 40/2.0
        return control
    }()
    
    
    fileprivate lazy var select_sign_view:UIView = {
       let c = UIView()
        c.backgroundColor = UIColor.init(hex: 0xFEC207)
        c.cornerRadius = 42/2.0
        c.alpha = 0
        return c
    }()
    
    var model:ST_GiveGiftCrowdCellModel?{
        didSet{
            
            guard let m = model else {
                return
            }
            
            avatar_image_view.pin_setImage(from: m.avatar.compress_small_image(width: 40*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            select_sign_view.alpha = m.isSelected ? 1 : 0
        }
    }
}


class ST_GiveGiftCrowdCellModel {
    
    let accid:String
    let avatar:String
    var isSelected:Bool
    
    init(accid:String,avatar:String,isSelected:Bool) {
        self.accid = accid
        self.avatar = avatar
        self.isSelected = isSelected
    }
}
